import Template from "./basics";
import { FORM_ITEM_TEMPLATE, RULES_TEMPLATE, FORM_DATA_TEMPLATE } from './const'

export default class EditTemplate extends Template {
    constructor() {
        super()
        this.fields = []
    }

    setInfoApi({ url, method }) {
        return this.setParams({
            infoUrl: url,
            infoMethod: method
        })
    }

    setEditApi({ url, method }) {
        return this.setParams({
            editUrl: url,
            editMethod: method
        })
    }
    
    setAddApi({ url, method }) {
        return this.setParams({
            addUrl: url,
            addMethod: method
        })
    }

    setFields(fields) {
        this.fields = fields
        return this.setForm()
    }

    setForm() {
        let formData = ''
        let formHtml = ''
        let formRules = ''
        for (let i = 0; i < this.fields.length; i++) {
            const field = this.fields[i]
            const template = new Template().setParams(field)

            formData += template.render(FORM_DATA_TEMPLATE)
            formHtml += template.render(FORM_ITEM_TEMPLATE)
            formRules += template.render(RULES_TEMPLATE)
        }
        return this.setParams({
            formData,
            formHtml,
            formRules
        })
    }
}